package models

import (
	"gorm.io/gorm"
)

// InterestCircle 兴趣圈模型
type InterestCircle struct {
	gorm.Model
	Name        string `json:"name" gorm:"size:100;not null;unique"`
	Description string `json:"description" gorm:"type:text"`
	Icon        string `json:"icon" gorm:"size:255"` // 像素图标URL
	CreatorID   uint   `json:"creator_id"`           // 圈主ID
	MemberCount int    `json:"member_count" gorm:"default:1"`
	Status      string `json:"status" gorm:"size:20;default:'active'"` // active, closed
	Private     bool   `json:"private" gorm:"default:true"`            // 是否私有圈子
}

// CircleMember 圈子成员关系
type CircleMember struct {
	gorm.Model
	CircleID uint   `json:"circle_id"`
	UserID   uint   `json:"user_id"`
	Role     string `json:"role" gorm:"size:20;default:'member'"`   // owner, admin, member
	Status   string `json:"status" gorm:"size:20;default:'active'"` // active, banned
	JoinDate int64  `json:"join_date"`                              // 加入时间戳
}

// CreateInterestCircle 创建新兴趣圈
func CreateInterestCircle(circle *InterestCircle) error {
	return GetDB().Create(circle).Error
}

// GetCircleByID 根据ID获取兴趣圈
func GetCircleByID(id uint) (*InterestCircle, error) {
	var circle InterestCircle
	result := GetDB().Where("id = ?", id).First(&circle)
	return &circle, result.Error
}

// GetCirclesByUserID 获取用户加入的所有圈子
func GetCirclesByUserID(userID uint) ([]InterestCircle, error) {
	var circles []InterestCircle
	result := GetDB().Table("interest_circles").
		Joins("JOIN circle_members ON interest_circles.id = circle_members.circle_id").
		Where("circle_members.user_id = ? AND circle_members.status = ?", userID, "active").
		Find(&circles)
	return circles, result.Error
}
